package com.lzlg.lzlglab.utils;


import lombok.Data;

/**
 * @Description: 自定义的统一结果响应
 * @Author: GX Cui
 * @Date 09:50 2024/7/17
 */
@Data
public class Response<T> {
    private Integer code;
    private String msg;
    private T data;
    private Long timestamp;
    // 响应成功不带数据
    public static Response ok(){
        return ok(null);
    }
    // 响应成功携带数据
    public static <T> Response<T> ok(T data){
        return error(ResponseCode.SUCCESS,data);
    }

    // 响应失败 统一的标准
    public static Response error(){
        return error(ResponseCode.failure);
    }

    // 响应失败 使用枚举中定义的状态
    public static Response error(ResponseCode responseCode){
        return error(responseCode, null);
    }

    // 响应失败 携带信息（自定义）
    public static <T> Response<T> error(ResponseCode responseCode,T data){
        Response<T> response = new Response<>();
        response.setCode(responseCode.getCode());
        response.setMsg(responseCode.getMsg());
        response.setData(data);
        response.setTimestamp(System.currentTimeMillis());
        return response;
    }
}
